<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../../res/layui/css/layui.css" media="all">
    <script src="../../../res/js/jquery-3.4.1.min.js"></script>
    <script src="../../../res/js/util.js"></script>
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>


<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>生产计划维护</legend>
</fieldset>

<form class="layui-form" action="" lay-filter="forms">
    <div class="layui-form-item">
        <label class="layui-form-label">计划名称</label>
        <div class="layui-input-block">
            <input type="text"  name="planName" maxlength="30" autocomplete="off"  lay-verify="required" lay-reqtext="不能为空" placeholder="请输入名称"
                   class="layui-input" >
            <input type="hidden" id="planId" name="planId">
        </div>
    </div>


    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">开始时间</label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" readonly lay-verify="required" lay-reqtext="不能为空" autocomplete="off" name="planKtime" id="test5" placeholder="yyyy-MM-dd">
            </div>
        </div>

        <div class="layui-inline">
            <label class="layui-form-label">结束时间</label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" readonly lay-verify="required" lay-reqtext="不能为空" autocomplete="off" name="planJtime" id="test6" placeholder="yyyy-MM-dd">
            </div>
        </div>
    </div>


    <div class="layui-form-item">
        <div class="layui-form-item">
            <label class="layui-form-label">操作人</label>
            <div class="layui-input-block">
                <input type="text" id="oper" name="oper"    placeholder="请输入操作人" autocomplete="off"
                       class="layui-input layui-disabled" disabled="">
            </div>
        </div>
    </div>

    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">计划说明</label>
        <div class="layui-input-block">
            <textarea placeholder="请输入内容" maxlength="2000" name="remark" class="layui-textarea"></textarea>
        </div>
    </div>


    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" id="dd" class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
        </div>
    </div>
</form>

<script src="../../../res/layui/layui.js" charset="utf-8"></script>


<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script>

    var form = null;
    var layer = null;
    var layedit = null;
    var laydate = null;
    var util = null;
    var transfer = null;

    layui.use(['form', 'layedit', 'laydate', 'transfer', 'layer', 'util'], function () {


        form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate
            , util = layui.util
            , transfer = layui.transfer;

        getData();
        //日期
        //日期时间选择器
      var startDate =   laydate.render({
            elem: '#test5'
            ,type: 'datetime'
            ,min: 1
            ,trigger: 'click'
            ,done: function(value,date){
              endDate.config.min={
                  year:date.year,
                  month:date.month-1,//关键
                  date: date.date+1

              }
              var startTime  = value.replace("-","/");//替换字符，变成标准格式
              var endTime = $("#test6").val().replace("-","/")
              var d1 = new Date(Date.parse(startTime));
              var d2 = new Date(Date.parse(endTime));


              if(d1>=d2){
                  layer.msg("开始时间不能大于结束时间");
                  $("#test5").val("");
              }
          }
        });


       var endDate = laydate.render({
            elem: '#test6'
            ,type: 'datetime'
            ,min: 1
            ,trigger: 'click'
            ,max:"2099-12-31"//设置一个默认最大值
            ,btns: ['clear', 'confirm']
            ,done: function(value, date){
               startDate.config.max ={
                   year:date.year,
                   month:date.month-1, //关键
                   date: date.date-1
               };

           }
        });




        //创建一个编辑器

        //自定义验证规则
        form.verify({
            title: function (value) {
                if (value.length < 5) {
                    return '用户至少得5个字符啊';
                }
            }
            , pass: [
                /^[\S]{6,12}$/
                , '密码必须6到12位，且不能出现空格'
            ]
            , content: function (value) {
                layedit.sync(editIndex);
            }
        });

        //监听指定开关

        var s=0;
        //监听提交
        form.on('submit(demo1)', function (data) {
            s+=1;
           if(s==1){
               var loa ;
               var url = "../../../hqPlan/insert";
               $.ajax({
                   url:url,
                   data:data.field,
                   dataType: 'json',
                   type:'post',
                   beforeSend:function(){
                       loa = layer.load(0,{shade:false,time: 4*1000});
                   },
                   success:function (obj) {
                       layer.msg(obj.message)
                       if (obj.status == 20) {
                           layer.close(loa);
                           //关闭当前弹出层
                           parent.closeOpen(obj.message);
                       }else{
                           s=0;
                       }
                   }
               })
           }
/*
            $.post(url, data.field, function (obj) {

            }, "json")*/
            return false;
        });




    });

    function getData(){


        let planId = getQueryString("id");
        if (planId) {
            console.info("取到的id为" + planId);
            var url = "../../../hqPlan/selectOne";
            var data = {id: planId}

            $.ajax({
                type: "get",
                url: url,
                cache: false,
                async: false,
                data: data,
                dataType: 'json',
                success: function (obj) {
                    form.val("forms", obj)
                    //渲染
                    form.render("select")
                }

            });
        }else{
            let users = window.localStorage.getItem("users");
            let data = JSON.parse(users);
            // console.info(data)
            $("#oper").val(data.userName);

        }
    }

</script>

</body>
</html>